import { Component } from '@angular/core';
import { NavController, NavParams, ToastController } from 'ionic-angular';
import { LoginDataProvider } from '../../providers/login-data/login-data';
import { User } from '../../providers/login-data/user';
import { Storage } from '@ionic/storage';
import { TabsPage } from '../tabs/tabs';

@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  user:User;

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public toastCtrl: ToastController,
    public loginDataProvider: LoginDataProvider,
    public storage:Storage) { 
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  /**
   * 登录
   * @param username 
   * @param passWord 
   * @param remember 
   */
  login(username:HTMLInputElement,passWord:HTMLInputElement,remember:HTMLInputElement){
    let _username = username.value.trim();
    let _password = passWord.value.trim();
    let _remember = remember.value;
    if (_username.length === 0) {
      this.showToast('bottom','请输入账号');
      return;
    }
    if (_password.length === 0) {
      this.showToast('bottom','请输入密码');
      return;
    }

    this.loginDataProvider.login(_username,_password)
      .toPromise()
      .then(res=>{
        console.log('登录成功');
        console.log(res);
        this.user=res;
        return true;
      },e=>{
        console.log('登录失败');
        console.log(e);
        return false;
      }).then(res=>{
        if (res) {
          let data = {
            username:this.user.username,
            onjectId:this.user.objectId,
            sessionToKen:this.user.sessionToken,
            remember:_remember
          };
          // 保存“记住”的用户信息
          if (_remember) {
            this.storage.remove('USER_INFO');
            this.storage.set('USER_INFO',JSON.stringify(data));
          }
          // 保存“登录”的用户信息
          this.storage.set('LOGIN_INFO',JSON.stringify(data));
          this.navCtrl.setRoot(TabsPage);
        }else{
          this.showToast('bottom','用户名或密码错误');
        }
      });
  }

  /**
   * 注册
   * @param username 
   * @param passWord 
   */
  register(username:HTMLInputElement,passWord:HTMLInputElement){

    let _username = username.value.trim();
    let _password = passWord.value.trim();
    if (_username.length === 0) {
      this.showToast('bottom','账号不能为空');
      return;
    }
    if (_password.length === 0) {
      this.showToast('bottom','密码不能为空');
      return;
    }

    this.loginDataProvider.register(_username,_password)
    .toPromise()
      .then(res=>{
        console.log('注册成功');
        console.log(res);
        this.user=res;
        return true;
      },e=>{
        console.log('注册失败');
        console.log(e);
        return false;
      }).then(res=>{
        if (res) {
          this.showToast('bottom','注册成功');
          this.navCtrl.setRoot(this.navCtrl.getActive().component);
        }else{
          this.showToast('bottom','账号已存在！');
        }
      }
      );
  }

  /**
   * 显示提示信息
   * @param position 
   * @param message 
   */
  showToast(position:string,message:string){
    let toast = this.toastCtrl.create({
      message:message,
      duration:2000,
      position:position
    });
    toast.present();
  }

}
